package com.sbw.tinysoft.Utils;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * 简单工具集合
 * Created by sbw on 2017/3/11.
 */
public class TinyUtil {

    public static String user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";

    /**
     * map转对象
     *
     * @param map  map
     * @param type 类型
     * @param <T>  泛型
     * @return 对象
     * @throws Exception 反射异常
     */
    static <T> T mapToBean(Map<String, Object> map, Class<T> type) throws Exception {
        if (map == null) {
            return null;
        }
        Set<Map.Entry<String, Object>> sets = map.entrySet();
        T entity = type.newInstance();
        Method[] methods = type.getDeclaredMethods();
        for (Map.Entry<String, Object> entry : sets) {
            String str = entry.getKey();
            String setMethod = "set" + str.substring(0, 1).toUpperCase() + str.substring(1);
            for (Method method : methods) {
                if (method.getName().equals(setMethod)) {
                    method.invoke(entity, entry.getValue());
                }
            }
        }
        return entity;
    }

    /**
     * service 列表链接
     * @param preUrl
     * @return
     */
    public static String serviceListUrl(String preUrl){
        return preUrl+"&"+currentDate();
    }

    /**
     * &service=com.lvmama.visa.api.service.VisaDocService&dayStr=2017-12-06
     * @param preUrl
     * @return
     */
    public static String methodListUrl(String preUrl,String service){
        return preUrl+"&dayStr="+currentDate()+"&service="+service;
    }

    /**
     * &service=com.lvmama.visa.api.service.VisaDocService&method=findBizVisaDetailTemplateListFromCache&dayStr=2017-12-06
     * @param preUrl
     * @return
     */
    public static String ipListUrl(String preUrl,String service,String method){
        return preUrl+"&dayStr="+currentDate()+"&service="+service+"&method="+method;
    }


    public static String currentDate(){
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String strDate=sdf.format(date);
        return strDate;
    }
}